package com.seg.system.service.impl;

import com.seg.common.core.domain.model.LoginUser;
import com.seg.common.exception.ServiceException;
import com.seg.common.utils.DateUtils;
import com.seg.common.utils.bean.BeanValidators;
import com.seg.system.domain.TOutWarehouse;
import com.seg.system.domain.TReturnWarehouse;
import com.seg.system.domain.TSparePart;
import com.seg.system.mapper.TOutWarehouseMapper;
import com.seg.system.mapper.TReturnWarehouseMapper;
import com.seg.system.mapper.TSparePartMapper;
import com.seg.system.service.ITReturnWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.Date;
import java.util.List;

/**
 * 备件退库Service业务层处理
 *
 * @author seg
 * @date 2023-07-04
 */
@Service
public class TReturnWarehouseServiceImpl implements ITReturnWarehouseService {
    @Autowired
    private TReturnWarehouseMapper tReturnWarehouseMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private TSparePartMapper tSparePartMapper;
    @Autowired
    private TOutWarehouseMapper tOutWarehouseMapper;

    /**
     * 查询备件退库
     *
     * @param id 备件退库主键
     * @return 备件退库
     */
    @Override
    public TReturnWarehouse selectTReturnWarehouseById(Long id) {
        return tReturnWarehouseMapper.selectTReturnWarehouseById(id);
    }

    /**
     * 查询备件退库列表
     *
     * @param tReturnWarehouse 备件退库
     * @return 备件退库
     */
    @Override
    public List<TReturnWarehouse> selectTReturnWarehouseList(TReturnWarehouse tReturnWarehouse) {
        return tReturnWarehouseMapper.selectTReturnWarehouseList(tReturnWarehouse);
    }

    /**
     * 新增备件退库
     *
     * @param tReturnWarehouse 备件退库
     * @return 结果
     */
    @Override
    public int insertTReturnWarehouse(TReturnWarehouse tReturnWarehouse, String operName) {
        tReturnWarehouse.setCreateTime(DateUtils.getNowDate());
        BeanValidators.validateWithException(validator, tReturnWarehouse);
        tReturnWarehouse.setCreateBy(operName);
        tReturnWarehouse.setDelFlag("0");
        //判断物料状态1，如果当前库存低于安全库存时 ；物料状态2，如果当前库存低于SAP库存时
        //根据物料号查询数据
        TSparePart t = tSparePartMapper.getPartByMaterialNo(tReturnWarehouse.getMaterialNo());
        if (t == null) {
            return -1;
        }
        Long curStock = t.getStock() + tReturnWarehouse.getOutNum();
        if (curStock < t.getSapStock()) {
            t.setStatus2("CLSAP");
        } else {
            t.setStatus2("");
        }
        if (curStock < t.getSafetyStock()) {
            t.setStatus1("CLS");
        } else {
            t.setStatus1("");
        }
        t.setStock(curStock);
        int i = tSparePartMapper.updateTSparePart(t);
        int j = tReturnWarehouseMapper.insertTReturnWarehouse(tReturnWarehouse);
        if (i == 0 || j == 0) {
            throw new ServiceException("退库失败");
        }
        return 1;
    }

    /**
     * 修改备件退库
     *
     * @param tReturnWarehouse 备件退库
     * @return 结果
     */
    @Override
    public int updateTReturnWarehouse(TReturnWarehouse tReturnWarehouse) {
        tReturnWarehouse.setUpdateTime(DateUtils.getNowDate());
        return tReturnWarehouseMapper.updateTReturnWarehouse(tReturnWarehouse);
    }

    /**
     * 批量删除备件退库
     *
     * @param ids 需要删除的备件退库主键
     * @return 结果
     */
    @Override
    public int deleteTReturnWarehouseByIds(Long[] ids) {
        return tReturnWarehouseMapper.deleteTReturnWarehouseByIds(ids);
    }

    /**
     * 删除备件退库信息
     *
     * @param id 备件退库主键
     * @return 结果
     */
    @Override
    public int deleteTReturnWarehouseById(Long id) {
        return tReturnWarehouseMapper.deleteTReturnWarehouseById(id);
    }

    @Override
    public int partReturnApproval1(Long[] ids, String operName) {
        if (ids == null || ids.length < 1) {
            return -1;
        }
        List<TOutWarehouse> list = tOutWarehouseMapper.selectTReturnWarehouseByIds(ids);
        if (list == null || list.isEmpty()) {
            return -2;
        }
        if (list.size() != ids.length) {
            return -2;
        }
        for (TOutWarehouse outWarehouse : list) {
            outWarehouse.setStatus(2);
            outWarehouse.setApproval1By(operName);
            int i = tOutWarehouseMapper.updateTOutWarehouse(outWarehouse);
            if(i!=1){
                throw new RuntimeException("一级审批失败");
            }
        }
        return 1;
    }

    @Transactional
    @Override
    public int partReturnApproval2(Long[] ids, String operName) {
        if (ids == null || ids.length < 1) {
            return -1;
        }
        List<TOutWarehouse> list = tOutWarehouseMapper.selectTReturnWarehouseByIds(ids);
        if (list == null || list.isEmpty()) {
            return -2;
        }
        if (list.size() != ids.length) {
            return -2;
        }
        Date create = DateUtils.getNowDate();
        for (TOutWarehouse outWarehouse : list) {
            TReturnWarehouse tReturnWarehouse = new TReturnWarehouse();
            tReturnWarehouse.setMaterialNo(outWarehouse.getMaterialNo());
            tReturnWarehouse.setLine(outWarehouse.getLine());
            tReturnWarehouse.setStation(outWarehouse.getStation());
            tReturnWarehouse.setOutNum(outWarehouse.getOutNum());
            tReturnWarehouse.setCreateTime(create);
            tReturnWarehouse.setCreateBy(operName);
            tReturnWarehouse.setDelFlag("0");
            //判断物料状态1，如果当前库存低于安全库存时 ；物料状态2，如果当前库存低于SAP库存时
            //根据物料号查询数据
            TSparePart t = tSparePartMapper.getPartByMaterialNo(tReturnWarehouse.getMaterialNo());
            if (t == null) {
                return -1;
            }
            Long curStock = t.getStock() + outWarehouse.getOutNum();
            if (curStock < t.getSapStock()) {
                t.setStatus2("CLSAP");
            } else {
                t.setStatus2("");
            }
            if (curStock < t.getSafetyStock()) {
                t.setStatus1("CLS");
            } else {
                t.setStatus1("");
            }
            t.setStock(curStock);
            outWarehouse.setStatus(3);
            outWarehouse.setApproval2By(operName);
            int i = tOutWarehouseMapper.updateTOutWarehouse(outWarehouse);
            int j = tSparePartMapper.updateTSparePart(t);
            int k = tReturnWarehouseMapper.insertTReturnWarehouse(tReturnWarehouse);
            if (i == 0 || j == 0 || k == 0) {
                throw new ServiceException("退库失败");
            }

        }

        return 1;
    }

    @Transactional
    @Override
    public int tReturnWarehouse(Long[] ids, String operName) {
        if (ids == null || ids.length < 1) {
            return -1;
        }
        List<TOutWarehouse> list = tOutWarehouseMapper.selectTReturnWarehouseByIds(ids);
        if (list == null || list.isEmpty()) {
            return -2;
        }
        if (list.size() != ids.length) {
            return -2;
        }
        for (TOutWarehouse outWarehouse : list) {
            outWarehouse.setStatus(1);
            outWarehouse.setReturnRequestBy(operName);
            int i = tOutWarehouseMapper.updateTOutWarehouse(outWarehouse);
            if(i==0){
                throw new RuntimeException("退库申请失败");
            }
        }
        return 1;
    }
}
